/****************************************************************************

Force Engine v0.5

Creado: 28/03/08
Clase: MyScene.h
Hecho by: German Battiston AKA Melkor

****************************************************************************/

//---------------------------------------------------------------------------
#include "MyScene.h"
//---------------------------------------------------------------------------
MyScene::MyScene(Graphics &rkGraphics)
:
Scene(rkGraphics),
m_pkGraphics(&rkGraphics),
m_pkCamera(NULL),
m_pkMesh(NULL),
m_pkRenderTexture(NULL),
m_pkRenderSurface(NULL),
m_pkBackBuffer(NULL),
m_VertexBuffer(NULL)
{
	
}
//---------------------------------------------------------------------------
bool MyScene::OnInit()
{	
	m_kContext = cgCreateContext();

	m_pszProgram = "void green( float3 position : POSITION, out float4 oPosition : POSITION, out float4 oColor: COLOR, uniform float3 color, uniform float4x4 modelWorldViewProj ){oPosition = mul(float4(position,1), modelWorldViewProj);	oColor    = float4(color,1);}";
	
	m_kCgProgram = cgCreateProgram(m_kContext, CG_SOURCE, m_pszProgram, CG_PROFILE_VS_2_0, "green", 0);

	m_pkGraphics->GetDevice()->CreateTexture(512,
											 512,
											 1,
											 D3DUSAGE_RENDERTARGET,
											 D3DFMT_R5G6B5,
											 D3DPOOL_DEFAULT,
											 &m_pkRenderTexture,
											 NULL);

	m_pkRenderTexture->GetSurfaceLevel(0, &m_pkRenderSurface);

	D3DXMatrixIdentity(&m_kViewMat);
	D3DXMatrixIdentity(&m_kWorldMat);
	D3DXMatrixOrthoLH(&m_kProjectionMat, 3.0f, 3.0f, -10.0f, 10.0f);
	//D3DXMatrixPerspectiveFovLH(&m_kMatPerspective, D3DX_PI / 4.0f, 1, 1, 100);

	m_pkGraphics->GetDevice()->GetRenderTarget(0 ,&m_pkBackBuffer);

	cgD3D9EnableDebugTracing(true);
	CGerror error = cgGetError();
	const char* errorString = cgGetErrorString(error);
	
	cgD3D9SetDevice(m_pkGraphics->GetDevice());
	cgD3D9LoadProgram(m_kCgProgram, false, 0);

	CreatePlane();

	HRESULT hr = m_pkGraphics->GetDevice()->CreateVertexBuffer( 4 * sizeof(TextureVertex),
																D3DUSAGE_WRITEONLY,
																D3DFVF_TEXVERTEX,
																D3DPOOL_DEFAULT,
																&m_VertexBuffer,
																NULL );

	void * pVertices = NULL;

	hr = m_VertexBuffer->Lock(0, sizeof(TextureVertex) * 4, (void**)&pVertices, 0);
	memcpy(pVertices, m_pkTexVertexPoligon, sizeof(TextureVertex) * 4);
	hr = m_VertexBuffer->Unlock();

	m_pkMesh = new Mesh(*m_pkGraphics);
	
	if(!m_pkMesh->LoadMesh("Ship_06.X"))
	{
		return false;
	}

	m_pkMesh->Initialize();

	m_pkCamera = new Camera(0.0f, 0.0f, -5.0f);

	return true;
}
//---------------------------------------------------------------------------
bool MyScene::OnUpdate(float fTimeBetweenFrames)
{
	static float fpSpeed = 0.01f;

	Color.x = 0.0f;
	Color.y = 0.0f;
	Color.y = 1.0f;
	Color.w = 1.0f;

	if(m_pkInput->GetKeyDown(DIK_D))
	{
		m_pkCamera->Strafe(fpSpeed);
	}
	
	if(m_pkInput->GetKeyDown(DIK_A))
	{
		m_pkCamera->Strafe(-fpSpeed);
	}

	if(m_pkInput->GetKeyDown(DIK_W))
	{
		m_pkCamera->Walk(fpSpeed);
	}

	if(m_pkInput->GetKeyDown(DIK_S))
	{
		m_pkCamera->Walk(-fpSpeed);
	}

	if(m_pkInput->GetKeyDown(DIK_SPACE))
	{
		m_pkCamera->Fly(fpSpeed);
	}

	if(m_pkInput->GetKeyDown(DIK_LCONTROL))
	{
		m_pkCamera->Fly(-fpSpeed);
	}
		
	m_pkCamera->Yaw((float)m_pkInput->GetMouseRelPosX() * 0.007f);
	m_pkCamera->Pitch((float)m_pkInput->GetMouseRelPosY() * 0.007f);

	return true;
}
//---------------------------------------------------------------------------
void MyScene::OnDraw()
{
	m_pkGraphics->GetDevice()->SetRenderTarget(0, m_pkRenderSurface);

	m_pkGraphics->GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
									 D3DCOLOR_XRGB(255, 0, 0), 1.0f, 0);
	m_pkGraphics->BeginScene();

	m_pkGraphics->SetCamera(m_pkCamera);

	m_pkMesh->Draw();

	m_pkGraphics->GetDevice()->GetTransform(D3DTS_VIEW, &m_kOldViewMat);
	//m_pkGraphics->GetDevice()->GetTransform(D3DTS_WORLD, &m_kOldWorldMat);
	m_pkGraphics->GetDevice()->GetTransform(D3DTS_PROJECTION, &m_kOldProjectionMat);

	m_pkGraphics->GetDevice()->SetTexture(0, m_pkRenderTexture);

	/*m_pkGraphics->GetDevice()->SetTransform(D3DTS_VIEW, &m_kViewMat);
	m_pkGraphics->GetDevice()->SetTransform(D3DTS_WORLD, &m_kWorldMat);
	m_pkGraphics->GetDevice()->SetTransform(D3DTS_PROJECTION, &m_kProjectionMat);*/
	
	WorldViewProjection = m_kOldViewMat*m_kOldProjectionMat;

	cgD3D9SetUniform(cgGetNamedParameter(m_kCgProgram, "modelWorldViewProj"), &WorldViewProjection);
	cgD3D9SetUniform(cgGetNamedParameter(m_kCgProgram, "color"), &Color);
	
	cgD3D9BindProgram(m_kCgProgram);
									  
	m_pkGraphics->EndScene();

	m_pkGraphics->GetDevice()->SetVertexShader(NULL);
	m_pkGraphics->GetDevice()->SetRenderTarget(0, m_pkBackBuffer);

	m_pkGraphics->GetDevice()->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 
									 D3DCOLOR_XRGB(0, 255, 255), 1.0f, 0);
	m_pkGraphics->BeginScene();

	m_pkGraphics->GetDevice()->SetStreamSource(0, m_VertexBuffer, 0, sizeof(TextureVertex));
	m_pkGraphics->GetDevice()->SetFVF(D3DFVF_TEXVERTEX);
	m_pkGraphics->GetDevice()->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

	m_pkGraphics->GetDevice()->SetTransform(D3DTS_VIEW, &m_kViewMat);
	m_pkGraphics->GetDevice()->SetTransform(D3DTS_WORLD, &m_kWorldMat);
	m_pkGraphics->GetDevice()->SetTransform(D3DTS_PROJECTION, &m_kProjectionMat);

	m_pkGraphics->EndScene();

	m_pkGraphics->Present();
}
//---------------------------------------------------------------------------
bool MyScene::OnDeInit()
{
	m_pkRenderSurface->Release();
	m_pkRenderSurface = NULL;

	m_pkBackBuffer->Release();
	m_pkBackBuffer = NULL;

	delete m_pszProgram;
	m_pszProgram = NULL;

	delete m_pkCamera;
	m_pkCamera = NULL;

	delete m_pkMesh;
	m_pkMesh = NULL;

	m_pkGraphics = NULL;

	cgD3D9SetDevice(0);
	cgDestroyProgram(m_kCgProgram);
	cgDestroyContext(m_kContext);

	return true;
}
//---------------------------------------------------------------------------
MyScene::~MyScene()
{
	
}
//---------------------------------------------------------------------------
void MyScene::CreatePlane()
{
	m_pkTexVertexPoligon[0].x = -1.0f;
	m_pkTexVertexPoligon[0].y =	1.0f;
	m_pkTexVertexPoligon[0].z =	0.0f;

	m_pkTexVertexPoligon[0].tu = 0.0f;
	m_pkTexVertexPoligon[0].tv = 0.0f;

	m_pkTexVertexPoligon[1].x =	1.0f;
	m_pkTexVertexPoligon[1].y =	1.0f;
	m_pkTexVertexPoligon[1].z =	0.0f;
	
	m_pkTexVertexPoligon[1].tu = 1.0f;
	m_pkTexVertexPoligon[1].tv = 0.0f;

	m_pkTexVertexPoligon[2].x = -1.0f;
	m_pkTexVertexPoligon[2].y = -1.0f;
	m_pkTexVertexPoligon[2].z =	0.0f;
	
	m_pkTexVertexPoligon[2].tu = 0.0f;
	m_pkTexVertexPoligon[2].tv = 1.0f;

	m_pkTexVertexPoligon[3].x =	1.0f;
	m_pkTexVertexPoligon[3].y = -1.0f;
	m_pkTexVertexPoligon[3].z =	0.0f;
	
	m_pkTexVertexPoligon[3].tu = 1.0f;
	m_pkTexVertexPoligon[3].tv = 1.0f;
}
//---------------------------------------------------------------------------